/*  1:   */ package nl.siegmann.epublib.bookprocessor;
/*  2:   */ 
/*  3:   */ import java.io.ByteArrayOutputStream;
/*  4:   */ import java.io.File;
/*  5:   */ import java.io.IOException;
/*  6:   */ import java.io.OutputStreamWriter;
/*  7:   */ import java.io.Writer;
/*  8:   */ import javax.xml.parsers.DocumentBuilder;
/*  9:   */ import javax.xml.parsers.DocumentBuilderFactory;
/* 10:   */ import javax.xml.transform.Result;
/* 11:   */ import javax.xml.transform.Source;
/* 12:   */ import javax.xml.transform.Transformer;
/* 13:   */ import javax.xml.transform.TransformerConfigurationException;
/* 14:   */ import javax.xml.transform.TransformerException;
/* 15:   */ import javax.xml.transform.TransformerFactory;
/* 16:   */ import javax.xml.transform.dom.DOMSource;
/* 17:   */ import javax.xml.transform.stream.StreamResult;
/* 18:   */ import javax.xml.transform.stream.StreamSource;
/* 19:   */ import nl.siegmann.epublib.domain.Book;
/* 20:   */ import nl.siegmann.epublib.domain.Resource;
/* 21:   */ import nl.siegmann.epublib.epub.BookProcessor;
/* 22:   */ import nl.siegmann.epublib.epub.EpubProcessorSupport;
/* 23:   */ import org.slf4j.Logger;
/* 24:   */ import org.slf4j.LoggerFactory;
/* 25:   */ import org.w3c.dom.Document;
/* 26:   */ import org.xml.sax.InputSource;
/* 27:   */ 
/* 28:   */ public class XslBookProcessor
/* 29:   */   extends HtmlBookProcessor
/* 30:   */   implements BookProcessor
/* 31:   */ {
/* 32:43 */   private static final Logger log = LoggerFactory.getLogger(XslBookProcessor.class);
/* 33:   */   private Transformer transformer;
/* 34:   */   
/* 35:   */   public XslBookProcessor(String xslFileName)
/* 36:   */     throws TransformerConfigurationException
/* 37:   */   {
/* 38:48 */     File xslFile = new File(xslFileName);
/* 39:49 */     TransformerFactory transformerFactory = TransformerFactory.newInstance();
/* 40:50 */     this.transformer = transformerFactory.newTransformer(new StreamSource(xslFile));
/* 41:   */   }
/* 42:   */   
/* 43:   */   public byte[] processHtml(Resource resource, Book book, String encoding)
/* 44:   */     throws IOException
/* 45:   */   {
/* 46:55 */     byte[] result = null;
/* 47:   */     try
/* 48:   */     {
/* 49:57 */       DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
/* 50:58 */       DocumentBuilder db = dbFactory.newDocumentBuilder();
/* 51:59 */       db.setEntityResolver(EpubProcessorSupport.getEntityResolver());
/* 52:   */       
/* 53:61 */       Document doc = db.parse(new InputSource(resource.getReader()));
/* 54:   */       
/* 55:63 */       Source htmlSource = new DOMSource(doc.getDocumentElement());
/* 56:64 */       ByteArrayOutputStream out = new ByteArrayOutputStream();
/* 57:65 */       Writer writer = new OutputStreamWriter(out, "UTF-8");
/* 58:66 */       Result streamResult = new StreamResult(writer);
/* 59:   */       try
/* 60:   */       {
/* 61:68 */         this.transformer.transform(htmlSource, streamResult);
/* 62:   */       }
/* 63:   */       catch (TransformerException e)
/* 64:   */       {
/* 65:70 */         log.error(e.getMessage(), e);
/* 66:71 */         throw new IOException(e);
/* 67:   */       }
/* 68:73 */       return out.toByteArray();
/* 69:   */     }
/* 70:   */     catch (Exception e)
/* 71:   */     {
/* 72:76 */       throw new IOException(e);
/* 73:   */     }
/* 74:   */   }
/* 75:   */ }


/* Location:           C:\Users\David\workspace\CosaRaraPorqueSi\main\epublib-tools-3.1.jar
 * Qualified Name:     nl.siegmann.epublib.bookprocessor.XslBookProcessor
 * JD-Core Version:    0.7.0.1
 */